<?php


namespace app\common\model;

use app\model\AdminStaff;
use app\model\AdminStaffIcCard;
use app\model\AdminStudentDormitory;

/**
 * 学生模型
 * Class StudentModel
 * @package app\common\model
 */
class StudentModel extends AdminStaff
{
    /**
     * 获取所有学生的年级数据
     * @return array|false
     */
    public static function gradeList()
    {
        $list = (new static())
            ->where('is_delete', 0)
            ->whereNotNull('grade')
            ->group('grade')->column('grade');
        $grade = [];
        foreach ($list as $item) {
            $grade[$item] = [
                'label' => "{$item}级",
                'value' => $item
            ];
        }
        return array_values($grade);
    }

    /**
     * @notes: 获取学生已授权的宿舍间信息
     * @param $staff_id
     * @return AdminStaffIcCard[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function dormitoryBelongStudent($staff_id)
    {
        $icCards = AdminStaffIcCard::where([
            'staff_id' => $staff_id,
            'is_delete' => 0
        ])->column('student_dormitory_id');
        $list = AdminStudentDormitory::where('id', 'in', $icCards)->where('is_delete', 0)
            ->field('id,uuid,name')->select()->toArray();

        return $list;
    }

    /**
     * @param $selectedArr ['id' => [0,1,2,3]] 0.蓝牙开锁 1.临时密码 2.超级SIM卡 3.IC卡
     * @param $staff_id
     * @desc 学生宿舍间授权
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author: speed
     * @date: 2022/8/17 16:08
     */
    public static function lockAuthorization($selectedArr, $staff_id)
    {
        if (!is_array($staff_id)) {
            $staff_id = [$staff_id];
        }
        $staffList = AdminStaff::where([
            'id' => $staff_id,
            'is_delete' => 0
        ])->field('id,smart_card')
            ->select()->toArray();
        if (!$staffList) {
            throw new \Exception('账户未找到');
        }

        $select_id_arr = [];
        foreach ($selectedArr as $item) {
            $select_id_arr[$item['id']] = $item['permission'];
        }
        $staff_id = array_column($staffList, 'id');

        // 删除学生未授权的宿舍楼
        $icCards = AdminStaffIcCard::where([
            'staff_id' => $staff_id,
            'is_delete' => 0
        ])->select();
        foreach ($icCards as $card) {
            if (!empty($select_id_arr[$card->student_dormitory_id])) {
                $card->lock_permission = implode(",", $select_id_arr[$card->student_dormitory_id]);
                $card->save();
                unset($select_id_arr[$card->student_dormitory_id]);
            } else {
                $card->is_delete = 1;
                $card->save();
            }
        }
        if (count($select_id_arr) > 0) {
            // 批量保存学生宿舍号
            foreach ($staffList as $item) {
                AdminStaffIcCard::batchInsert($item['id'], $select_id_arr, $item['smart_card']);
            }
        }
    }
}